package com.tang.transmission.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tang.common.entity.community.Article;
import com.tang.common.entity.liveVideo.LiveInfo;
import com.tang.common.entity.liveVideo.VideoInfo;
import com.tang.transmission.mapper.LiveInfoMapper;
import com.tang.transmission.service.VideoInfoService;
import com.tang.transmission.mapper.VideoInfoMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 唐朝阳
 * @since 2022-05-17
 */
@Service
public class VideoInfoServiceImpl extends ServiceImpl<VideoInfoMapper, VideoInfo> implements VideoInfoService {
    @Autowired
    private VideoInfoMapper videoInfoMapper;
    @Override
    public PageInfo<VideoInfo> listVideoInfo(String keywords, Integer currentPage, Integer pageSize,Integer hasAuth) {
        PageHelper.startPage(currentPage,pageSize);
        if (keywords == null || "".equals(keywords)) {
            QueryWrapper<VideoInfo> qw = new QueryWrapper<VideoInfo>();
            qw.eq("delete_flag",1);
            qw.eq("state",hasAuth);
            qw.orderByDesc("create_time");
            return PageInfo.of(videoInfoMapper.selectList(qw));
        }
        QueryWrapper<VideoInfo> queryWrapper = new QueryWrapper<VideoInfo>();
        queryWrapper.eq("delete_flag",1).
                eq("state",hasAuth).orderByDesc("create_time").
                like("video_title",keywords).or().like("institutions",keywords);
        List<VideoInfo> lives = videoInfoMapper.selectList(queryWrapper);
        return  PageInfo.of(lives);
    }

    @Override
    public PageInfo<VideoInfo> listCompanyVideo(String keywords, Integer currentPage, Integer pageSize, String companyName) {
        PageHelper.startPage(currentPage,pageSize);
        if (keywords == null || "".equals(keywords)) {
            QueryWrapper<VideoInfo> qw = new QueryWrapper<VideoInfo>();
            qw.eq("delete_flag",1);
            qw.orderByDesc("create_time");
            qw.eq("institutions",companyName);
            return PageInfo.of(videoInfoMapper.selectList(qw));
        }
        QueryWrapper<VideoInfo> queryWrapper = new QueryWrapper<VideoInfo>();
        queryWrapper.eq("delete_flag",1).orderByDesc("create_time").
               eq("institutions",companyName).
                like("video_title",keywords);
        List<VideoInfo> lives = videoInfoMapper.selectList(queryWrapper);
        return  PageInfo.of(lives);
    }

    @Override
    public String auditVideo(Integer videoId, String res) {
        VideoInfo videoInfo = videoInfoMapper.selectById(videoId);
        if ("pass".equals(res) && videoInfo != null) {
            videoInfo.setState(1);
            int i = videoInfoMapper.updateById(videoInfo);
            if (i != 0) {
                return "pass";
            }
        }
        if ("noPass".equals(res) && videoInfo != null) {
            videoInfo.setState(2);
            int i = videoInfoMapper.updateById(videoInfo);
            if (i != 0) {
                return "noPass";
            }
        }
        return null;
    }
}
